package com.global.college.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.os.Build;
import androidx.appcompat.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.View;

import com.global.college.CollegeApplication;
import com.global.college.utils.ScreenUtils;

/**
 * @author: xjh
 * @datetime: 2021/9/30
 */
public class RoundImageview extends AppCompatImageView {
    float width, height;

    int dp5 = ScreenUtils.dp2px(CollegeApplication.getContext(), 5);

    public RoundImageview(Context context) {
        this(context, null);
        init(context, null);
    }

    public RoundImageview(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        init(context, attrs);
    }

    public RoundImageview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        if (Build.VERSION.SDK_INT < 18) {
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        width = getWidth();
        height = getHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (width >= dp5 && height > dp5) {
            Path path = new Path();
            //四个圆角
            path.addCircle(width / 2, width / 2, width / 2, Path.Direction.CCW);
            canvas.clipPath(path);
        }
        super.onDraw(canvas);
    }

}
